/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-04-27
*/
package com.rzico.account.controller.member;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.account.entity.CardBill;
import com.rzico.account.entity.Deposit;
import com.rzico.account.entity.MatchPoint;
import com.rzico.account.service.MatchPointService;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.entity.Member;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.basics.service.MemberService;
import com.rzico.core.service.SysSequenceService;
import com.rzico.entity.PageResult;
import com.rzico.entity.Pageable;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 金币账单控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-04-27
 */
@Api(description = "金币账单接口")
@RestController
@RequestMapping("/member/matchPoint")
public class MatchPointMemberController extends BaseController {

    @Autowired
    private MatchPointService matchPointService;

    @Autowired
    private MemberService memberService;

    @Autowired
    private EnterpriseService enterpriseService;

    @Autowired
    private SysSequenceService sysSequenceService;

    /**
     * 分页查询金币账单
     *
     * @return
     */
    @ApiOperation("分页查询金币账单")
    @GetMapping("/list")
    @ApiImplicitParams({@ApiImplicitParam(name = "month", value = "月份", dataType = "String", paramType = "query")
    })
    public CommResult<CardBill> list(Long cardId, String month, Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);
        Member member = memberService.getCurrent();

        params.put("memberId", member.getId());
        if (month!=null) {
            String m = month.concat("-01");
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date d = sdf.parse(m);
                d = DateUtils.addMonths(d, 1);
                d = DateUtils.addMilliseconds(d, -1);
                params.put("endDate", d);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<MatchPoint> list = matchPointService.selectList(params);
        PageResult<MatchPoint> pageResult = new PageResult<MatchPoint>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }

}
